<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\cms\home;

use app\index\controller\Home;
use app\cms\model\Column as ColumnModel;
use app\cms\model\Message as MessageModel;
use app\cms\model\Vote as VoteModel;
use app\cms\model\Document as DocumentModel;
use think\Db;
/**
 * 前台首页控制器
 * @package app\cms\admin
 */
class Help extends Home
{
    public function _initialize(){
        parent::_initialize();
        $catedata=ColumnModel::getSTreeList();
        $this->assign("catedata",$catedata);
    }
    /**
     * 首页
     * @author 陈梦晨 <741459065@qq.com>
     * @return mixed
     */
    public function index()
    {
        $columndata=ColumnModel::getTList();
        foreach ($columndata as $key => $value) {
            # code...
            $ids=ColumnModel::getChildsId($value['id']);
            $ids[]=$value['id'];
            $columndata[$key]['subdata']=Db::name("cms_document")->where(['cid'=>['in',$ids],'flag'=>['like','%c%'],'status'=>1])->limit(9)->field('id,title')->select();
        }
        $this->assign("columndata",$columndata);

        $noticedata=Db::name("cms_document")->where(['cid'=>13,'status'=>1])->field('id,title')->select();
        $this->assign("noticedata",$noticedata);

        return $this->fetch(); // 渲染模板
    }

    /**
     * 常见问题
     * @return mixed
     */
    public function question($pcatid=null,$catid=null,$subcatid=null)
    {
        if (!empty($pcatid)) {
            $catid=ColumnModel::getFirstCid($pcatid);
        }

        if ($catid === null) {
            $catid=ColumnModel::getFirstCid();
        }
        $this->assign("catid",$catid);

        $nav=ColumnModel::getParentsNav($catid);
        $this->assign("nav",$nav);

        $navids=ColumnModel::getParentsId($catid);
        $this->assign("navids",implode(",",$navids));

        $subcate=ColumnModel::where('pid',$catid)->order(['sort'=>'desc','id'=>'asc'])->field('id,name')->select();
        $this->assign("subcate",$subcate);

        $map=$order=[];
        $map['status']=1;
        $map['cid']=$catid;
        if(!empty($subcatid)){
            $map['cid']=$subcatid;
        }
        $order=['id'=>'desc'];
        $data = Db::view('cms_document', true)
            ->view("cms_article", 'content', 'cms_article.aid=cms_document.id', 'left')
            ->where($map)
            ->order($order)
            ->paginate(8);
        $this->assign("data",$data);
        $page=$data->render(); 
        $this->assign("page",$page);
        $isAjax=input('isAjax');
        if($isAjax){
            $this->view->engine->layout(false);
            return $this->fetch('_morelist');
        }else{
            return $this->fetch(); // 渲染模板
        }
        
    }

    /**
     * 常见问题详情
     * @author 陈梦晨 <741459065@qq.com>
     * @return mixed
     */
    public function show($id=null)
    {
        if ($id === null) $this->error('参数错误');

        $data=DocumentModel::getOne($id);
        $data['votenum']=VoteModel::getvotenum($id);
        $this->assign("data",$data);

        $nav=ColumnModel::getParentsNav($data['cid']);
        $this->assign("nav",$nav);

        $navids=ColumnModel::getParentsId($data['cid']);
        $this->assign("navids",implode(",",$navids));

        $reasonlist=Db::name("cms_attr")->where(['code'=>'reason'])->value('typedetail');
        $reasonlist=parse_attr($reasonlist);
        $this->assign("reasonlist",$reasonlist);
        return $this->fetch(); // 渲染模板
    }

    /**
     * 联系客服
     * @author 陈梦晨 <741459065@qq.com>
     * @return mixed
     */
    public function contact()
    {
        return $this->fetch(); // 渲染模板
    }

    /**
     * 新手指南
     * @author 陈梦晨 <741459065@qq.com>
     * @return mixed
     */
    public function guide()
    {
        $tagdata=Db::name("cms_field")->where(['name'=>'tag'])->value('options');
        $tagdata=parse_attr($tagdata);
        $data=[];
        foreach ($tagdata as $key => $value) {
            # code...
            $data[$key]['tag']=$value;
            $data[$key]['subdata']=Db::name("cms_document")->alias("a")->join("cms_article b","a.id=b.aid",'left')->where(['b.tag'=>$key,'a.status'=>1])->limit(9)->field('a.id,a.title')->select();
        }
        $this->assign("data",$data);
        return $this->fetch(); // 渲染模板
    }
    /**
     * 搜索
     * @author 陈梦晨 <741459065@qq.com>
     * @return mixed
     */
    public function search()
    {
        $keyword = input('keyword');
        $map=$order=[];
        $map['status']=1;
        $map['title|description|content']=['like',"%".$keyword."%"];
        $order=['id'=>'desc'];
        $data = Db::view('cms_document', true)
            ->view("cms_article", 'content', 'cms_article.aid=cms_document.id', 'left')
            ->where($map)
            ->order($order)
            ->paginate(8,false,['query'=>['keyword'=>$keyword]]);
        $this->assign("data",$data);
        $page=$data->render(); 
        $this->assign("page",$page);
        $isAjax=input('isAjax');
        if($isAjax){
            $this->view->engine->layout(false);
            return $this->fetch('_morelist');
        }else{
            return $this->fetch(); // 渲染模板
        }
    }
    /**
     * 在线留言
     * @author 陈梦晨 <741459065@qq.com>
     * @return mixed
     */
    public function message()
    {
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            $data['ip']=get_client_ip();

            // 验证
            $result = $this->validate($data, 'Message');
            // 验证失败 输出错误信息
            if(true !== $result) return $this->error($result);

            if ($menu = MessageModel::create($data)) {
                return $this->success('提交成功', url('guide'));
            } else {
                return $this->error('提交失败');
            }
        }
        return $this->fetch(); // 渲染模板
    }
    /**
     * 未解决原因提交
     * @author 陈梦晨 <741459065@qq.com>
     * @return mixed
     */
    public function vote()
    {
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            $data['ip']=get_client_ip();

            // 验证
            $result = $this->validate($data, 'Vote');
            // 验证失败 输出错误信息
            if(true !== $result) return json(['code'=>0,'msg'=>$result]);

            if ($menu = VoteModel::create($data)) {
                return json(['code'=>1,'msg'=>'提交成功']);
            } else {
                return json(['code'=>0,'msg'=>'提交失败']);
            }
        }else{
            return json(['code'=>0,'msg'=>'请求失败']);
        }
    }
}